package com.zyw.mvc.support;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;

/**
 * @author zhangyw
 * @date 16/9/6 16:02
 */
@JsonSerialize(using = WrappedResultSerialize.class)
//@JsonDeserialize(using = WrappedResultDeserialize.class)
public class WrappedResult {

    private int code; // 1 返回成功，－1 返回失败，即服务端异常
    private String msg; // 返回正常提示消息内容
    private String errMsg; //  返回异常提示消息内容，方便调试
    private Object data;

    public WrappedResult() {
        super();
    }

    /**
     * 生成处理成功的处理结果
     *
     * @Date 16/9/8
     * @Author zhangyw
     */
    public static WrappedResult handleSuccessWrappedResult(Integer code, String msg, Object data) {
        WrappedResult wrappedResult = new WrappedResult();
        wrappedResult.setCode(code == null ? 1 : code);
        wrappedResult.setMsg(msg == null ? "处理成功" : msg);
        wrappedResult.setData(data);
        return wrappedResult;
    }

    /**
     * 生成处理成功的结果
     *
     * @Date 16/9/8
     * @Author zhangyw
     */
    public static WrappedResult handleSuccessWrappedResult(Object data) {
        return handleSuccessWrappedResult(1, null, data);
    }


    /**
     * 业务异常 输出msg
     *
     * @Date 16/9/7
     * @Author zhangyw
     */
    public static WrappedResult handleExceptionWrappedResult(Integer code, String msg, String errMsg) {
        WrappedResult wrappedResult = new WrappedResult();
        wrappedResult.setCode(code == null ? -1 : code);
        wrappedResult.setMsg(msg == null ? "请求异常" : msg);
        wrappedResult.setErrMsg(errMsg);
        return wrappedResult;
    }

    /**
     * 系统错误异常，输出errMsg
     *
     * @Date 16/9/7
     * @Author zhangyw
     */
    public static WrappedResult handleExceptionWrappedResult(String errMsg) {
        return handleExceptionWrappedResult(-1, null, errMsg);
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public String getErrMsg() {
        return errMsg;
    }

    public void setErrMsg(String errMsg) {
        this.errMsg = errMsg;
    }
}
